package com.example.androidtest3

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class fifthActivity : AppCompatActivity() {
    var seconds=0//计时
    var running=false//判断运行
    var wasRunning=false//判断是否正在运行

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_fifth)
        if(savedInstanceState!=null){//进入时先去判断页面转换情况
            seconds=savedInstanceState.getInt("seconds")
            running=savedInstanceState.getBoolean("running")
            wasRunning=savedInstanceState.getBoolean("wasrunning")

        }
        runTimer()
        val button_stop=findViewById<Button>(R.id.button_stop)
        val button_start=findViewById<Button>(R.id.button_start)
        val button_restart=findViewById<Button>(R.id.button_restart)
        val finish=findViewById<Button>(R.id.finish)
        button_start.setOnClickListener {
            running=true
        }
        button_stop.setOnClickListener {
            running=false
        }
        button_restart.setOnClickListener {
            running=false
            seconds=0
        }
        finish.setOnClickListener {
            finish()
        }
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
    fun runTimer(){
        val handle= Handler()//创建一个线程，让主线程运行时run从这里面获取
        val textView=findViewById<TextView>(R.id.textView_timer)
        val runnable=object:Runnable{
            override fun run(){
                val hours=seconds/3600
                val minutes=(seconds%3600)/60
                val secs=seconds%60
                textView.text=String.format("%02d:%02d:%02d",hours,minutes,secs)
                if(running){//正在运行的话seconds值加1
                    seconds ++
                }
                handle.postDelayed(this,1000)//计时的速度
            }
        }
        handle.post(runnable)//放入线程的任务去调用并执行
    }

    //实现屏幕变换后仍然在计时，使用生命周期表示，保存状态函数
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        //保存状态
        outState.putInt("seconds",seconds)//保存秒
        outState.putBoolean("running",running)//保存运行状态
        outState.putBoolean("wasrunning",wasRunning)//保存正在运行状态
    }

    //从后台回到前台，如果wasrunning=true，就没有在页面中继续计时，相反，在退出之前没有选择暂停就继续计时
    override fun onResume() {
        super.onResume()
        Log.d("life style","onResume")
        if(wasRunning){
            running=false
        }
    }
    //退出后台之后停止计时
    override fun onPause() {
        super.onPause()
        Log.d("life style","onPause")
        wasRunning=running
        running=false
    }
}